blktap: fix empty QCOW images (bug 1430 part 2)
authorKeir Fraser <keir.fraser@citrix.com>
Wed, 3 Jun 2009 10:11:50 +0000 (11:11 +0100)
committerKeir Fraser <keir.fraser@citrix.com>
Wed, 3 Jun 2009 10:11:50 +0000 (11:11 +0100)
Empty QCOW images consist of only the L1 table, this results in a
file size which is not sector-aligned. Since blktap uses O_DIRECT, the
block aligned read of the L1 table will go beyond the end of file and
thus returns the actual file size and not the expected length.
This patch checks whether at least the L1 table has been read.

This should fix bug 1430.

Signed-off-by: Andre Przywara <andre.przywara@amd.com>
tools/blktap/drivers/block-qcow.c

index dd65cd01bbf950ea115bb07484d277493b2afc21..8027fcaca222d408ddf5e6f18cdeb45d19ee6581 100644 (file)
@@ -824,7 +824,7 @@ static int tdqcow_open (struct disk_driver *dd, const char *name, td_flag_t flag
        l1_table_block = ROUNDUP(l1_table_block, 512);
        ret = posix_memalign((void **)&buf2, 4096, l1_table_block);
        if (ret != 0) goto fail;
-       if (read(fd, buf2, l1_table_block) != l1_table_block)
+       if (read(fd, buf2, l1_table_block) < l1_table_size + s->l1_table_offset)
                goto fail;
        memcpy(s->l1_table, buf2 + s->l1_table_offset, l1_table_size);
 
@@ -878,7 +878,8 @@ static int tdqcow_open (struct disk_driver *dd, const char *name, td_flag_t flag
 
                        memcpy(buf2 + s->l1_table_offset, s->l1_table, l1_table_size);
                        lseek(fd, 0, SEEK_SET);
-                       if (write(fd, buf2, l1_table_block) != l1_table_block) {
+                       if (write(fd, buf2, l1_table_block) < 
+                               l1_table_size + s->l1_table_offset) {
                                DPRINTF("qcow: Failed to write new L1 table\n");
                                goto fail;
                        }